package com.yky.utils;


import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringApplicationContext {

	private static ApplicationContext instance = null;

	private SpringApplicationContext() {

	}

	public static ApplicationContext getInstance(ServletContext servletContext) {
		if (instance == null) {
			instance = WebApplicationContextUtils
					.getWebApplicationContext(servletContext);
		}
		return instance;
	}

	public static ApplicationContext getInstanceByXML() {
		if (instance == null) {
			instance = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
		}
		return instance;
	}

	public static Object getService(String serviceBeanName) {
		if (instance == null) {
			SpringApplicationContext.getInstanceByXML();
		}
		return instance.getBean(serviceBeanName);
	}
}
